package cn.gemiman.daoyi.sgm.cmat;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.List;

@Slf4j
public class SgmCmatSqlGeneratorUtils {
    @SneakyThrows
    public static void main(String[] args) {
        System.out.println(System.currentTimeMillis());
        if (true)
            return;
        final String sqlTemp = "INSERT INTO OWCMAT.TT_SGM_D2C_SALE_DATA(ID,SALE_CODE,SERIES_ID,SERIES_NAME,NEW_INTENTION_NUM,INTENTION_RETURN_NUM,NEW_ORDER_NUM,DELIVERY_NUM,UNSUBSCRIBE_NUM,RETAINED_ORDER_NUM," +
                "REPORT_DATE,REPORT_YEAR,REPORT_WEEK,REPORT_MONTH,CREATE_DATE,CREATE_BY,UPDATE_DATE,UPDATE_BY,REPORT_QUARTER,WECHAT_ADD_NUM,NEW_INVITED_NUM) " +
                "VALUES (owcmat.sq_sgm_d2c_sale_data.nextval,'CH1018',2267,'探界者Plus（PHEV）',0,0,0,0,0,0," +
                "to_date('{}','yyyy-MM-dd hh24:mi:ss')," +
                "{}," +
                "{}," +
                "{}," +
                "to_date('{}','yyyy-MM-dd hh24:mi:ss')," +
                "'sql',NULL,NULL," +
                "{}," +
                "0,0);";
        DateTime dateTime = DateUtil.beginOfYear(DateUtil.date());
        log.info("begin:{}", DateUtil.formatDateTime(dateTime));
        int week = DateUtil.weekOfYear(dateTime);
        log.info("week:{}", week);
        DateTime endDate = DateUtil.parseDate("2024-03-28");
        log.info("end:{}", DateUtil.formatDateTime(endDate));
        List<String> sqlList = CollUtil.toList();
        while (dateTime.before(endDate)) {
            log.info("current:{}", DateUtil.formatDateTime(dateTime));
            DateTime REPORT_DATE = dateTime;
            int REPORT_YEAR = DateUtil.year(dateTime);
            int REPORT_WEEK = DateUtil.weekOfYear(dateTime);
            int REPORT_MONTH = DateUtil.month(dateTime) + 1;
            DateTime CREATE_DATE = dateTime;
            int REPORT_QUARTER = DateUtil.quarter(dateTime);
            System.out.println(StrUtil.format(
                    "REPORT_DATE:{},\nREPORT_YEAR:{},\nREPORT_WEEK:{},\nREPORT_MONTH:{},\nCREATE_DATE:{},\nREPORT_QUARTER:{}",
                    DateUtil.formatDateTime(REPORT_DATE),
                    REPORT_YEAR,
                    REPORT_YEAR + StrUtil.fillBefore(REPORT_WEEK + "", '0', 2),
                    REPORT_YEAR + StrUtil.fillBefore(REPORT_MONTH + "", '0', 2),
                    DateUtil.formatDateTime(CREATE_DATE),
                    REPORT_YEAR + StrUtil.fillBefore(REPORT_QUARTER + "", '0', 2)
            ));
            String currentSql = StrUtil.format(sqlTemp,
                    DateUtil.formatDateTime(REPORT_DATE),
                    REPORT_YEAR,
                    REPORT_YEAR + StrUtil.fillBefore(REPORT_WEEK + "", '0', 2),
                    REPORT_YEAR + StrUtil.fillBefore(REPORT_MONTH + "", '0', 2),
                    DateUtil.formatDateTime(CREATE_DATE),
                    REPORT_YEAR + StrUtil.fillBefore(REPORT_QUARTER + "", '0', 2));
            System.out.println("currentSql: " + currentSql);
            sqlList.add(currentSql);
            dateTime = DateUtil.offsetDay(dateTime, 1);
        }
        File file = FileUtil.newFile("/Users/gemiman/IdeaProjects/daoyicode/daoyi-cloud-pro/daoyi-server/src/test/java/cn/gemiman/daoyi/sgm/cmat/sql/init-data-20240819-." + RandomUtil.randomNumbers(10) + ".sql");
        if (file.createNewFile()) {
            FileUtil.appendLines(sqlList, file, StandardCharsets.UTF_8);
        }
    }
}
